/*
 * @Author: Xujianchen
 * @Date: 2024-08-01 11:00:49
 * @LastEditors: Xujianchen
 * @LastEditTime: 2024-08-01 11:21:52
 * @Description: 复制指令
 */
import { ElMessage } from 'element-plus'
import { copyToClipboard } from '@/utils/utils'

export default {
  mounted(el, binding) {
    el.copyData = binding.value
    // 事件名：click ｜ dblclick
    const eventName = binding.arg || 'click'
    el.addEventListener(eventName, handler)
  },
  updated(el, binding) {
    el.copyData = binding.value
  },
  beforeUnmount(el) {
    el.removeEventListener('click', handler)
  },
}

async function handler() {
  try {
    await copyToClipboard(this.copyData)
    ElMessage.success('复制成功')
  } catch (error) {
    ElMessage.error('复制失败')
    console.error(error)
  }
}
